package com.briup.ptp.utils;

import com.google.errorprone.annotations.Var;
import org.csource.common.MyException;
import org.csource.fastdfs.*;

import javax.sound.midi.Track;
import java.io.IOException;

/**
 * @author GaoB
 * @date 2024/2/28 21:38:24
 * @description fastdfs工具类
 */
public class FastDFSUtils {
    private static String basePath = "d:\\projectIMG\\";
    private static String IP = "http:\\\\152.136.143.158\\";

    public static String upLoad(String fileName) {
        StorageServer ss = null;
        TrackerServer ts = null;
        String[] split = fileName.split("[.]");
//        读取配置文件
        try {
            ClientGlobal.init("fastdfs.conf");
            TrackerClient tc = new TrackerClient();
            ts = tc.getConnection();
            ss = tc.getStoreStorage(ts);
//            定义Storage的客户端对象，使用该对象完成上传、下载、删除操作
            StorageClient sc = new StorageClient(ts, ss);
            String[] result = sc.upload_file(basePath+fileName, split[split.length-1], null);
            String link = IP + result[0] + "\\" + result[1];
            return link;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (MyException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if(ts != null){
                    ts.close();
                }
                if(ss != null){
                    ss.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
